﻿using System;
using System.Collections.Generic;
using System.Text;
using MyLib.Service;
using System.Web;
using MyLib.Configuration;
using MyLib.Utils;

namespace MyLib.Integration
{
    public class SSOUtil
    {
        public static void Logout()
        {
            HttpContext.Current.Session["__LoginedUser"] = null;
        }

        public static User LoginedUser
        {
            get
            {
                if (HttpContext.Current.Session["__LoginedUser"] != null)
                {
                    User user = HttpContext.Current.Session["__LoginedUser"] as User;
                    return user;
                }
                else
                {
                    string prefix = PortalUrl;
                    if (string.IsNullOrEmpty(prefix))
                    {
                        prefix = "..";
                    }
                    HttpContext.Current.Response.Redirect(prefix + "/Login.aspx?returnUrl=" + HttpContext.Current.Request.Url.AbsoluteUri);
                }
                return new User();
            }
            set
            {
                HttpContext.Current.Session["__LoginedUser"] = value;
            }
        }

        public static string PortalUrl
        {
            get
            {
                if (HttpContext.Current.Session["__PortalUrl"] == null)
                {
                    HttpContext.Current.Session["__PortalUrl"] = WebUtil.GetRequestWebSite(); 
                }
                return HttpContext.Current.Session["__PortalUrl"] as string;
            }
            set
            {
                HttpContext.Current.Session["__PortalUrl"] = value;
            }
        }

        public static List<AuthObject> AuthedObjects
        {
            get
            {
                return null;
            }
            set
            {
            }
        }
    }
}
